﻿using System;
using Marbles.Core.Connectors;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Marbles.Web.Admin.Downloads
{
    /// <summary>
    /// Summary description for Userlist
    /// </summary>
    public class Userlist : DownloadHandler
    {
        public override void Download(HttpResponse response)
        {
            Command cmd = new Command("SELECT a.username, a.fullname, a.email, a.lastlogin, a.openid, a.enabled FROM Users a WHERE siteid = @siteid ORDER BY a.username ASC");
            DataConnection dbc = new DataConnection();
            cmd.AddParameter("@siteid", this.CurrentWeb.SiteId);

            response.Write("Username,Fullname,Email,Last Login,OpenID,Enabled\n");
            foreach (DataRow dr in dbc.FetchDataTable(cmd).Rows)
            {
                response.Write(string.Format("\"{0}\",", dr["username"]));
                response.Write(string.Format("\"{0}\",", dr["fullname"]));
                response.Write(string.Format("\"{0}\",", dr["email"]));
                response.Write(string.Format("\"{0}\",", dr["lastlogin"]));
                response.Write(string.Format("\"{0}\",", dr["openid"]));
                response.Write(string.Format("\"{0}\"", (Convert.ToBoolean(dr["enabled"]) ? "YES" : "NO")));
                response.Write("\n");
            }
        }
    }
}